package test
{
	import _Pan3D.core.MathCore;
	import _Pan3D.display3D.Display3DContainer;
	import _Pan3D.display3D.Display3DMovie;
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.Md5Shader;
	import _Pan3D.program.shaders.StatShader;
	import _Pan3D.texture.TextureManager;
	
	import _me.Scene_data;
	
	import com.adobe.utils.PerspectiveMatrix3D;
	
	import flash.display.Loader;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import test.vo.TreeDisplay3DSprite;
	import test.vo.PersonSprite;
	import test.vo.StatVO;
	import test.vo.TreeShader;
	import test.vo.uvMove.Display3DUvMove;
	import test.vo.uvMove.UvMoveShader;
	
	[SWF(backgroundColor="#FFFFFF",width="1024", height="600", frameRate="60")]
	
	public class uvAndIndex extends BaseDemo
	{

		private var _programMd5:Program3D;
		private var _groundMode:Display3DContainer;
		private var _statItemMode:Display3DContainer;
		private var _treeItemMode:Display3DContainer;
		private var _frieItemMode:Display3DContainer;
		private var _guaiwuMd5Mode:PersonSprite;
		private var _tieXiaocontainer : Display3DContainer;
		
		public function uvAndIndex()
		{
			super();
		}
		override protected function makeScene():void
		{
			Scene_data._sunMatrix.identity();
			Scene_data._sunMatrix.prependTranslation(0, 0, -4000);
			Scene_data._sunMatrix.prependRotation(120, new Vector3D(1, 0, 0));
			Scene_data._sunMatrix.prependTranslation(0, 0, 0);
			var context3D : Context3D = Scene_data.context3D;
			TextureManager.getInstance().init(context3D);
			container=new Display3DContainer;
			_tieXiaocontainer=new Display3DContainer;
			
			addGrounds();
			addMd5Model();
			addListStat();
			
			
			this.addEventListener(Event.ENTER_FRAME, enterFrame);
			Scene_data.cam3D.distance = 300;
			Scene_data.focus3D.angle_y = 0;
			Scene_data.focus3D.angle_x =-30;
			
		}
		
		private function addListStat():void
		{
			_statItemMode=new Display3DContainer();
			container.addChild(_statItemMode);
			_treeItemMode=new Display3DContainer();
			container.addChild(_treeItemMode);
			_frieItemMode=new Display3DContainer();
			_tieXiaocontainer.addChild(_frieItemMode);
			var _loaderURL:URLRequest = new URLRequest("../_File/nijia/uvAndstatList.xml");
			var _scenexml:URLLoader=new URLLoader();
			var _statItem:Array=new Array;
			var context3D : Context3D = Scene_data.context3D;
			_scenexml.load(_loaderURL);
			_scenexml.addEventListener(Event.COMPLETE, _load_scene_ok);
			function _load_scene_ok(evt:Event):void
			{
				var _item:Array =MathCore.getSourcePro(XML(_scenexml.data));
				
				for(var  i:int=0;i<_item.length;i++){
					var statVO:StatVO=new StatVO;
					statVO.et=_item[i].et;
					statVO.scale=_item[i].scale;
					statVO.x=_item[i].x;
					statVO.y=_item[i].y;
					statVO.z=_item[i].z;
					statVO.type=_item[i].type;
					
					statVO.name=_item[i].name;
					_statItem.push(statVO);
					
					for(var j:int=0;j<_statItem.length;j++){
						var tempVo:StatVO=_statItem[j] as StatVO;
						
						if(tempVo.type==3){
							var sp2:TreeDisplay3DSprite = new TreeDisplay3DSprite(context3D);
							sp2.url =tempVo.et
							sp2.x=tempVo.x;
							sp2.y=tempVo.y;
							sp2.z=tempVo.z;
							sp2.scale=tempVo.scale;
							
							_treeItemMode.addChild(sp2);
						}else{
							var sp1:Display3DUvMove = new Display3DUvMove(context3D);
							sp1.url =tempVo.et
							sp1.x=tempVo.x;
							sp1.y=tempVo.y;
							sp1.z=tempVo.z;
							sp1.scale=tempVo.scale;
							_frieItemMode.addChild(sp1);
						}
						
					}
					
					var treeShaderProgram3d:Program3D = Program3DManager.getInstance().getProgram(TreeShader.TREE_SHADER);
					_treeItemMode.setProgram3D(treeShaderProgram3d);
					
					var statShaderProgram3d:Program3D = Program3DManager.getInstance().getProgram(UvMoveShader.UVMOVE_SHADER);
					_frieItemMode.setProgram3D(statShaderProgram3d);
					
					
				}
			}
			
		}
		
		private function addGrounds():void
		{
			
			var context3D : Context3D = Scene_data.context3D;
			_groundMode = new Display3DContainer();
			var sp1:Display3DSprite = new Display3DSprite(context3D);
			sp1.url = "baseGround";
			_groundMode.addChild(sp1);
			container.addChild(_groundMode);
			var statShaderProgram3d:Program3D = Program3DManager.getInstance().getProgram(StatShader.STATSHADER);
			_groundMode.setProgram3D(statShaderProgram3d);
			//sp1.visible=false;
		}
		private function addMd5Model():void
		{
			var context3D : Context3D = Scene_data.context3D;
			_guaiwuMd5Mode = new PersonSprite(context3D);
			_guaiwuMd5Mode.addMesh("0", "default");
			_guaiwuMd5Mode.addAnim("run", "run");
			_guaiwuMd5Mode.initStage(stage);
			//_guaiwuMd5Mode.addAnim("default", "default");
			_guaiwuMd5Mode.scale=0.2
			container.addChild(_guaiwuMd5Mode);
			var md5Program:Program3D = Program3DManager.getInstance().getProgram(Md5Shader.MD5SHADER);
			_guaiwuMd5Mode.setProgram3D(md5Program);
			
		}
		override protected function  enterFrame(event : Event) : void {
			//keyControl.upData();
			MathCore._catch_cam(Scene_data.cam3D, Scene_data.focus3D);
			if (!Scene_data.ready ) return;
			var context : Context3D = Scene_data.context3D;
			context.clear(0, 0, 0, 1);
			projMatrix = new PerspectiveMatrix3D();
			projMatrix.perspectiveFieldOfViewRH(1, 1, 0.1, 9000);
			context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, projMatrix, true);
			context.setBlendFactors(Context3DBlendFactor.ONE,Context3DBlendFactor.ZERO);
			container.update();
			context.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
			_tieXiaocontainer.update();
			context.present();
			
		}
	}
}